package com.hotelsystem.reservation.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JList;

import com.hotelsystem.reservations.constants.Constants;
import com.hotelsystem.reservations.layouts.ReservationListView;
import com.hotelsystem.reservations.reservations.Reservation;
import com.hotelsystem.reservations.reservations.ReservationManagement;

public class ReservationsListController implements ActionListener{
	private ReservationListView reservationListView;
	private ReservationManagement resManagement = new ReservationManagement();

	public ReservationsListController(ReservationListView reservationListView) {
		this.reservationListView = reservationListView;
	}
	
	public void loadAllReservations(){
		ArrayList<Reservation> currentRes;
		ArrayList<Reservation> pastRes;
		ArrayList<Reservation> futureRes;
		ArrayList<Reservation> allRes = new ArrayList<Reservation>();

		currentRes = resManagement.GetReservationsFromFile(Constants.CURRENT_RESERVATIONS_PATH);
		pastRes = resManagement.GetReservationsFromFile(Constants.PAST_RESERVATIONS_PATH);
		futureRes = resManagement.GetReservationsFromFile(Constants.FUTURE_RESERVATIONS_PATH);
		allRes.addAll(currentRes);
		allRes.addAll(pastRes);
		allRes.addAll(futureRes);
		
		loadReservations(allRes);
		
	}
	
	public void loadReservations(ArrayList<Reservation> hotelReservations){
		if(hotelReservations.isEmpty()){
			reservationListView.setReservationsList(new JList<Reservation>());
		}
		else{
			Reservation[] reservations = new Reservation[hotelReservations.size()]; 
			reservations =	hotelReservations.toArray(reservations);
			reservationListView.setReservationsList(new JList<Reservation>(reservations));
			reservationListView.getModel().removeAllElements();
			for(Reservation res : reservations){
				reservationListView.getModel().addElement(res);				
			}
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		ArrayList<Reservation> currentRes;
		ArrayList<Reservation> pastRes;
		ArrayList<Reservation> futureRes;
		switch(arg0.getActionCommand()){
		case "All": loadAllReservations();
					break;
		case "Current":
					currentRes = resManagement.GetReservationsFromFile(Constants.CURRENT_RESERVATIONS_PATH);
					if(currentRes.isEmpty()){
						loadReservations(new ArrayList<Reservation>());
					}
					else{
						loadReservations(currentRes);
					}
					break;
		case "Past":
					pastRes = resManagement.GetReservationsFromFile(Constants.PAST_RESERVATIONS_PATH);
					if(pastRes.isEmpty()){
						loadReservations(new ArrayList<Reservation>());
					}
					else{
						loadReservations(pastRes);
					}
					break;
		case "Future":
					futureRes = resManagement.GetReservationsFromFile(Constants.FUTURE_RESERVATIONS_PATH);
					if(futureRes.isEmpty()){
						loadReservations(new ArrayList<Reservation>());
					}
					else{
						loadReservations(futureRes);
					}
					break;
			
		}
		
	}

	public ReservationListView getReservationListView() {
		return reservationListView;
	}

	public void setReservationListView(ReservationListView reservationListView) {
		this.reservationListView = reservationListView;
	}
}
